package com.wp.www.service.impl;

import com.wp.www.entity.storage.Storage;
import com.wp.www.repository.StorageMapper;
import com.wp.www.service.StorageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class StorageServiceImpl implements StorageService {

    @Resource
    private StorageMapper storageMapper;

    @Override
    public Integer reduceStorage(String commodityCode, Integer count) {
        Storage storage = findStorageByCommodityCode(commodityCode);
        if (storage == null){
            throw new RuntimeException("该商品不存在！！");
        }else {
            Integer balanceCount = storage.getCount();
            if (balanceCount<count){
                throw new RuntimeException("当前库存不足！！！");
            }else {
                count = balanceCount - count;
            }
        }
        return storageMapper.reduceStorage(commodityCode,count);
    }

    @Override
    public Storage findStorageByCommodityCode(String commodityCode) {
        return storageMapper.findStorageByCommodityCode(commodityCode);
    }
}
